<?php

namespace App\Http\Controllers\Api;

use App\ActivityGroupAdd;
use App\ActivityGroupRefund;
use App\Orders;
use Faker\Provider\fr_CH\Payment;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use EasyWeChat\Factory;

class RefundController extends Controller
{
    /**
     * 改变拼团状态
     */
    public function refund(){
        /** @var ActivityGroupAdd $activityGroupAddParent */
        $activityGroupAddParents = ActivityGroupAdd::whereGroupId(0)->where('end_time','<=',date('Y-m-d H:i:s',time()))->whereStatus(0)->get();
        if(count($activityGroupAddParents) > 0){
            foreach($activityGroupAddParents as $activityGroupAddParent){
                $activityGroupAddParent->status = 2;
                $activityGroupAddParent->save();

                /** @var Orders $thisOrder */
                $thisOrder = Orders::whereOrderNum($activityGroupAddParent->order_num)->whereType('group')->whereStatus(8)->whereGroupRefund(0)->first();
                if($thisOrder){
                    $thisOrder->status = 4;
                    $thisOrder->save();

                    $refundResult = PaymentController::Refund($thisOrder,'拼团失败');

                    if($refundResult && $refundResult['return_code'] == 'SUCCESS' && $refundResult['result_code'] == 'SUCCESS'){
                        $newActivityGroupRefund = new ActivityGroupRefund();
                        $newActivityGroupRefund->user_id                           = $thisOrder->user_id;
                        $newActivityGroupRefund->order_num                         = $thisOrder->order_num;
                        $newActivityGroupRefund->reason                            = '拼团失败';
                        $newActivityGroupRefund->cancel_group                      = date('Y-m-d H:i:s',time());
                        $newActivityGroupRefund->save();

                    }else{
                        $newActivityGroupRefund = new ActivityGroupRefund();
                        $newActivityGroupRefund->user_id                           = $thisOrder->user_id;
                        $newActivityGroupRefund->order_num                         = $thisOrder->order_num;
                        $newActivityGroupRefund->reason                            = '拼团失败';
                        $newActivityGroupRefund->cancel_group                      = date('Y-m-d H:i:s',time());
                        $newActivityGroupRefund->status                            = 2;
                        $newActivityGroupRefund->failure_reason                    = isset($refundResult['err_code_des']) ? $refundResult['err_code_des'] : '申请退款失败';
                        $newActivityGroupRefund->save();

                        $thisOrder->group_refund = 2;
                        $thisOrder->save();
                    }
                }

                /** @var ActivityGroupAdd $activityGroupAddChild */
                $activityGroupAddChildes = ActivityGroupAdd::whereGroupId($activityGroupAddParent->id)->where('end_time','<=',date('Y-m-d H:i:s',time()))->whereStatus(0)->get();
                if(count($activityGroupAddChildes) > 0){
                    foreach($activityGroupAddChildes as $activityGroupAddChild){
                        $activityGroupAddChild->status = 2;
                        $activityGroupAddChild->save();

                        /** @var Orders $thisOrder */
                        $thisOrder = Orders::whereOrderNum($activityGroupAddChild->order_num)->whereType('group')->whereStatus(8)->whereGroupRefund(0)->first();
                        if($thisOrder){
                            $thisOrder->status = 4;
                            $thisOrder->save();

                            $refundResult = PaymentController::Refund($thisOrder,'拼团失败');

                            if($refundResult && $refundResult['return_code'] == 'SUCCESS' && $refundResult['result_code'] == 'SUCCESS'){
                                $newActivityGroupRefund = new ActivityGroupRefund();
                                $newActivityGroupRefund->user_id                           = $thisOrder->user_id;
                                $newActivityGroupRefund->order_num                         = $thisOrder->order_num;
                                $newActivityGroupRefund->reason                            = '拼团失败';
                                $newActivityGroupRefund->cancel_group                      = date('Y-m-d H:i:s',time());
                                $newActivityGroupRefund->save();

                            }else{
                                $newActivityGroupRefund = new ActivityGroupRefund();
                                $newActivityGroupRefund->user_id                           = $thisOrder->user_id;
                                $newActivityGroupRefund->order_num                         = $thisOrder->order_num;
                                $newActivityGroupRefund->reason                            = '拼团失败';
                                $newActivityGroupRefund->cancel_group                      = date('Y-m-d H:i:s',time());
                                $newActivityGroupRefund->status                            = 2;
                                $newActivityGroupRefund->failure_reason                    = isset($refundResult['err_code_des']) ? $refundResult['err_code_des'] : '申请退款失败';
                                $newActivityGroupRefund->save();

                                $thisOrder->group_refund = 2;
                                $thisOrder->save();
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 退款接口
     */
    public function refundResult(){
        /** @var ActivityGroupAdd $activityGroupAddParent */
        $activityGroupAddParents = ActivityGroupAdd::whereGroupId(0)->where('end_time','<=',date('Y-m-d H:i:s',time()))->whereStatus(2)->get();
        if(count($activityGroupAddParents) > 0){
            foreach($activityGroupAddParents as $activityGroupAddParent){
                /** @var Orders $thisOrder */
                $thisOrder = Orders::whereOrderNum($activityGroupAddParent->order_num)->whereType('group')->whereStatus(6)->whereGroupRefund(0)->first();
                if($thisOrder){
                    /** @var ActivityGroupRefund $thisActivityGroupRefund */
                    $thisActivityGroupRefund = ActivityGroupRefund::whereOrderNum($thisOrder->order_num)->whereStatus(0)->whereUserId($thisOrder->user_id)->first();
                    if($thisActivityGroupRefund){
                        $refundSelectResult = PaymentController::selectRefund($thisOrder);
                        if($refundSelectResult && $refundSelectResult['return_code'] == 'SUCCESS' && $refundSelectResult['result_code'] == 'SUCCESS'){
                            if($refundSelectResult['refund_status_0'] == 'SUCCESS'){
                                $thisActivityGroupRefund->status      = 1;
                                $thisActivityGroupRefund->refund_time = isset($refundSelectResult['refund_success_time_0']) ? $refundSelectResult['refund_success_time_0'] : date('Y-m-d H:i:s',time());
                                $thisActivityGroupRefund->save();

                                $thisOrder->group_refund = 1;
                                $thisOrder->save();
                            }elseif($refundSelectResult['refund_status_0'] == 'REFUNDCLOSE'){
                                $thisActivityGroupRefund->status      = 2;
                                $thisActivityGroupRefund->failure_reason = '退款关闭';
                                $thisActivityGroupRefund->save();

                                $thisOrder->group_refund = 2;
                                $thisOrder->save();
                            }elseif($refundSelectResult['refund_status_0'] == 'CHANGE'){
                                $thisActivityGroupRefund->status      = 2;
                                $thisActivityGroupRefund->failure_reason = '退款异常,可前往商户平台（pay.weixin.qq.com）-交易中心，手动处理此笔退款。';
                                $thisActivityGroupRefund->save();

                                $thisOrder->group_refund = 2;
                                $thisOrder->save();
                            }

                        }else{
                            $thisActivityGroupRefund->status         = 2;
                            $thisActivityGroupRefund->failure_reason = isset($refundSelectResult['err_code_des']) ? $refundSelectResult['err_code_des'] : '微信退款申请接收失败';
                            $thisActivityGroupRefund->save();
                        }
                    }
                }

                /** @var ActivityGroupAdd $activityGroupAddChild */
                $activityGroupAddChildes = ActivityGroupAdd::whereGroupId($activityGroupAddParent->id)->where('end_time','<=',date('Y-m-d H:i:s',time()))->whereStatus(2)->get();
                if(count($activityGroupAddChildes) > 0){
                    foreach($activityGroupAddChildes as $activityGroupAddChild){
                        /** @var Orders $thisOrder */
                        $thisOrder = Orders::whereOrderNum($activityGroupAddChild->order_num)->whereType('group')->whereStatus(6)->whereGroupRefund(0)->first();
                        if($thisOrder){
                            /** @var ActivityGroupRefund $thisActivityGroupRefund */
                            $thisActivityGroupRefund = ActivityGroupRefund::whereOrderNum($thisOrder->order_num)->whereStatus(0)->whereUserId($thisOrder->user_id)->first();
                            if($thisActivityGroupRefund){
                                $refundSelectResult = PaymentController::selectRefund($thisOrder);
                                if($refundSelectResult && $refundSelectResult['return_code'] == 'SUCCESS' && $refundSelectResult['result_code'] == 'SUCCESS'){
                                    if($refundSelectResult['refund_status_0'] == 'SUCCESS'){
                                        $thisActivityGroupRefund->status      = 1;
                                        $thisActivityGroupRefund->refund_time = isset($refundSelectResult['refund_success_time_0']) ? $refundSelectResult['refund_success_time_0'] : date('Y-m-d H:i:s',time());
                                        $thisActivityGroupRefund->save();

                                        $thisOrder->group_refund = 1;
                                        $thisOrder->save();
                                    }elseif($refundSelectResult['refund_status_0'] == 'REFUNDCLOSE'){
                                        $thisActivityGroupRefund->status      = 2;
                                        $thisActivityGroupRefund->failure_reason = '退款关闭';
                                        $thisActivityGroupRefund->save();

                                        $thisOrder->group_refund = 2;
                                        $thisOrder->save();
                                    }elseif($refundSelectResult['refund_status_0'] == 'CHANGE'){
                                        $thisActivityGroupRefund->status      = 2;
                                        $thisActivityGroupRefund->failure_reason = '退款异常,可前往商户平台（pay.weixin.qq.com）-交易中心，手动处理此笔退款。';
                                        $thisActivityGroupRefund->save();

                                        $thisOrder->group_refund = 2;
                                        $thisOrder->save();
                                    }

                                }else{
                                    $thisActivityGroupRefund->status         = 2;
                                    $thisActivityGroupRefund->failure_reason = isset($refundSelectResult['err_code_des']) ? $refundSelectResult['err_code_des'] : '微信退款申请接收失败';
                                    $thisActivityGroupRefund->save();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

